#!/usr/bin/env python3

# Copyright (c) 2020-2021 Fpemud <fpemud@sina.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.


import base64
from ._errors import FileFormatError
from ._handy import Handy


class AuthorizedKeyInfo:

    def __init__(self, line=None, key_type=None, key=None, comment=None):
        if line is not None:
            tlist = line.split()
            if tlist[0] not in Handy.KEY_TYPE_MAPPING:
                raise FileFormatError("options field is not supported")
            self._keyType = tlist[0]
            self._key = base64.b64decode(tlist[1])
            self._comment = tlist[2] if len(tlist) >= 3 else ""
        else:
            assert key_type in Handy.KEY_TYPE_MAPPING
            self._keyType = key_type
            self._key = key
            self._comment = comment

    @property
    def key_type(self):
        return Handy.KEY_TYPE_MAPPING[self._keyType]

    @property
    def key(self):
        return self._key

    @property
    def options(self):
        return ""

    @property
    def comment(self):
        return self._comment

    def _toLine(self):
        if self._comment != "":
            return "%s %s %s" % (self._keyType, base64.b64encode(self._key), self._comment)
        else:
            return "%s %s" % (self._keyType, base64.b64encode(self._key))


class KnownHostInfo:

    def __init__(self, line=None, host=None, key_type=None, key=None):
        if line is not None:
            tlist = line.split()
            if tlist[1] not in Handy.KEY_TYPE_MAPPING:
                raise FileFormatError("invalid key type")
            self._host = tlist[0]
            self._keyType = tlist[1]
            self._key = base64.b64decode(tlist[2])
        else:
            assert host is not None
            assert key_type in Handy.KEY_TYPE_MAPPING
            assert key is not None
            self._host = host
            self._keyType = key_type
            self._key = key

    @property
    def host(self):
        return self._host

    @property
    def key_type(self):
        return Handy.KEY_TYPE_MAPPING[self._keyType]

    @property
    def key(self):
        return self._key

    def _toLine(self):
        return "%s %s %s" % (self._host, self._keyType, base64.b64encode(self._key))
